package com.ihunanren.dao.ibatis.weixin;

import java.sql.SQLException;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.google.common.collect.Maps;
import com.ihunanren.dao.pojo.AppUserDO;
import com.ihunanren.dao.weixin.IAppUserDao;

/**
 * @author kalman03
 */
public class AppUserDao extends SqlMapClientTemplate implements IAppUserDao {

	@Override
	public AppUserDO getAppUserById(long id) throws SQLException {
		Object o = getSqlMapClient().queryForObject("AppUserDao.getAppUserById", id);
		if (o != null)
			return (AppUserDO) o;
		return null;
	}

	@Override
	public AppUserDO getAppUserByNick(String nick) throws SQLException {
		Object o = getSqlMapClient().queryForObject("AppUserDao.getAppUserByNick", nick);
		if (o != null)
			return (AppUserDO) o;
		return null;
	}

	@Override
	public AppUserDO getAppUserByEmail(String email) throws SQLException {
		Object o = getSqlMapClient().queryForObject("AppUserDao.getAppUserByEmail", email);
		if (o != null)
			return (AppUserDO) o;
		return null;
	}

	@Override
	public AppUserDO getAppUserByPhone(String phone) throws SQLException {
		Object o = getSqlMapClient().queryForObject("AppUserDao.getAppUserByPhone", phone);
		if (o != null)
			return (AppUserDO) o;
		return null;
	}

	@Override
	public boolean insertAppUser(AppUserDO appUserDO) throws SQLException {
		return getSqlMapClient().insert("AppUserDao.insertAppUser", appUserDO) != null;
	}

	@Override
	public boolean updateTokenById(long userId, String token)throws SQLException {
		Map<String, String> map = Maps.newHashMap();
		map.put("userId", userId + "");
		map.put("token", token);
		return getSqlMapClient().update("AppUserDao.updateTokenById", map) > 0;
	}

}
